using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

    public class CLSENCENT : ObservableObject
    {
        MDOgeneral mod = new MDOgeneral();

        #region Metodos basicos tabla
        
        public void consultar()
        {
            try
            {
                string query = "select TIPENT_EEN, HOJNRO_EEN, FECENT_EEN,CODALM_EEN, CONCEP_EEN, CODPRV_EEN, DOCREF_EEN from ENCENTP where TIPENT_EEN='" + TIPENT_EEN + "' and  HOJNRO_EEN='" + HOJNRO_EEN + "'";
                DataTable val = mod.consulta(query);
                if (val.Rows.Count > 0)
                {
                    this.TIPENT_EEN= val.Rows[0]["TIPENT_EEN"].ToString();
                    this.HOJNRO_EEN= val.Rows[0]["HOJNRO_EEN"].ToString();
                    this.FECENT_EEN= Convert.ToDateTime(val.Rows[0]["FECENT_EEN"]);
                    this.CODALM_EEN= val.Rows[0]["CODALM_EEN"].ToString();
                    this.CONCEP_EEN= val.Rows[0]["CONCEP_EEN"].ToString();
                    this.CODPRV_EEN = val.Rows[0]["CODPRV_EEN"].ToString();
                    this.DOCREF_EEN = val.Rows[0]["DOCREF_EEN"].ToString();
                    EXISTE = true;
                    CAMBIO = false;
                }
                else
                {
                    EXISTE = false;
                }
            }
            catch (Exception e) { throw e; }
        }

        public int actualizar()
        {
            try
            {
                string query = "update ENCENTP set FECENT_EEN='"+FECENT_EEN+"',CODALM_EEN='"+CODALM_EEN+"', CONCEP_EEN='"+CONCEP_EEN+"', CODPRV_EEN='"+CODPRV_EEN+"', DOCREF_EEN='"+DOCREF_EEN+"' where TIPENT_EEN='"+TIPENT_EEN+"' and HOJNRO_EEN='"+HOJNRO_EEN+"'";
                return mod.comando(query);
            }
            catch (Exception e) { throw e; }
        }

        public int insertar()
        {
            try
            {
                if (String.IsNullOrEmpty(HOJNRO_EEN) == true) { HOJNRO_EEN = " "; }//FJRF - Le quitamos el  null
                string query = "insert into ENCENTP (TIPENT_EEN, HOJNRO_EEN, FECENT_EEN,CODALM_EEN, CONCEP_EEN, CODPRV_EEN, DOCREF_EEN) values ('"+TIPENT_EEN+"', '"+HOJNRO_EEN+"', '"+FECENT_EEN.ToShortDateString()+"','"+CODALM_EEN+"', '"+CONCEP_EEN+"', '"+CODPRV_EEN+"', '"+DOCREF_EEN+"')";
                return mod.comando(query);
            }
            catch (Exception e) { throw e; }
        }

        public int eliminar()
        {
            try
            {
                 string query = "delete from ENCENTP where TIPENT_EEN='" + TIPENT_EEN + "' and HOJNRO_EEN='" + HOJNRO_EEN + "'";
                 return mod.comando(query);
             }
             catch (Exception e) { throw e; }
        }

        public Boolean existe()
        {
            try
            {
                string query = "select count(*) from ENCENTP where TIPENT_EEN='" + TIPENT_EEN + "' and  HOJNRO_EEN='" + HOJNRO_EEN + "'";
                return mod.consulta(query).Rows[0][0].ToString().Equals("0") ? false : true;    
            }
            catch (Exception e) { throw e; }
        }

        public int GdarEntMatPr(CLSENCENT encabezado,List<CLSDETENT> detalle)
        {  
            try 
            {
                MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();
        
                int j = 1;
                for (int i = 0; i <= detalle.Count - 1; i++)
                {
                        if (detalle[i].insertar() == 0)
                        {
                            j = 0;
                        }
                }
                encabezado.HOJNRO_EEN = ActConsHoja(encabezado);
                if (encabezado.insertar() == 0) {
                    j = 0;
                }
        
                MDOgeneral.transaccion.Commit();

                return j;

            }catch(Exception ex){ MDOgeneral.transaccion.Rollback(); throw (ex); }
        }

        public string ActConsHoja(CLSENCENT encabezado)
        {
            try 
            {
                clases.CLSCONTROLP valor = new clases.CLSCONTROLP();
                valor.ID_CON = "ENT";
                if (encabezado.TIPENT_EEN.Equals("N"))
                {
                    valor.COD_CON = "NOR";
                }
                else
                {
                    valor.COD_CON = "DEV";
                }
                valor.Consultar();
                string n = valor.NOM_CON;

                String StrConsecutivo;//FJRF - Variable para el manejo del consecutivo
                StrConsecutivo = valor.NOM_CON.Substring(2, 5);//FJRF - Separamos el consecutivo para ver que valos vamos a enviar

                int val = Convert.ToInt32(StrConsecutivo) + 1;
                string num = val.ToString();
                while (num.Length < 6) 
                {
                    num = "0" + num;
                }
                valor.NOM_CON=valor.NOM_CON.Replace(valor.NOM_CON.Substring(1, 6), num);
    
                valor.Actualizar();
    
                return n;

            }catch (Exception ex) { throw(ex); }
        }

        #endregion

        #region  Propiedades tabla 

        private string _TIPENT_EEN;
        public string TIPENT_EEN
        {
            get { return _TIPENT_EEN;}
            set { _TIPENT_EEN=value; RaisePropertyChangedEvent("TIPENT_EEN"); }
        }
    
        private string _HOJNRO_EEN;
        public string HOJNRO_EEN
        {
            get { return _HOJNRO_EEN;}
            set { _HOJNRO_EEN=value; RaisePropertyChangedEvent("HOJNRO_EEN"); }
        }

        private DateTime _FECENT_EEN=DateTime.Today;
        public DateTime FECENT_EEN
        {
            get { return _FECENT_EEN;}
            set 
            {
                if (_FECENT_EEN != value) { CAMBIO = true; }
                _FECENT_EEN=value;
                RaisePropertyChangedEvent("FECENT_EEN");
            }
        }

        private string _CODALM_EEN;
        public string CODALM_EEN
        {
            get { return _CODALM_EEN;}
            set 
            {
                if (_CODALM_EEN != value) { CAMBIO = true; }
                _CODALM_EEN=value;
                RaisePropertyChangedEvent("CODALM_EEN");
            }
        }

        private string _CONCEP_EEN;
        public string CONCEP_EEN
        {
            get { return _CONCEP_EEN;}
            set 
            {
                if (_CONCEP_EEN != value) { CAMBIO = true; }
                _CONCEP_EEN=value;
                RaisePropertyChangedEvent("CONCEP_EEN");
            }
        }

        private string _CODPRV_EEN;
        public string CODPRV_EEN
        {
            get { return _CODPRV_EEN;}
            set 
            {
                if (_CODPRV_EEN != value) { CAMBIO = true; }
                _CODPRV_EEN=value;
                RaisePropertyChangedEvent("CODPRV_EEN");
            }
        }

        private string _DOCREF_EEN;
        public string DOCREF_EEN
        {
            get { return _DOCREF_EEN; }
            set 
            {
                if (_DOCREF_EEN != value) { CAMBIO = true; }
                _DOCREF_EEN = value;
                RaisePropertyChangedEvent("DOCREF_EEN");
            }
        }
    
        private Boolean _CAMBIO=false;
        public Boolean CAMBIO
        {
            get { return _CAMBIO; }
            set { _CAMBIO = value; RaisePropertyChangedEvent("CAMBIO"); }
        }
    
        private Boolean _EXISTE=false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }

        #endregion
    }
}
